{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## GloVe embedding \n",
    "\n",
    "Download GloVe pre-trained embedding glove.6B.zip from: https://nlp.stanford.edu/projects/glove/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 187,
   "metadata": {},
   "outputs": [],
   "source": [
    "from __future__ import absolute_import\n",
    "from __future__ import division\n",
    "from __future__ import print_function\n",
    "\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 188,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loaded 400000 word vectors.\n"
     ]
    }
   ],
   "source": [
    "# load the whole embedding into memory\n",
    "embeddings = dict()\n",
    "f = open('glove.6B.200d.txt')\n",
    "for line in f:\n",
    "    values = line.split()\n",
    "    word = values[0]\n",
    "    embedding = np.array(values[1:], dtype='float32')\n",
    "    embeddings[word] = embedding\n",
    "f.close()\n",
    "print('Loaded %s word vectors.' % len(embeddings))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 253,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = embeddings['beijing']\n",
    "# print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 254,
   "metadata": {},
   "outputs": [],
   "source": [
    "y = embeddings['china']\n",
    "# print(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 255,
   "metadata": {},
   "outputs": [],
   "source": [
    "z = embeddings['philippines']\n",
    "# print(z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 256,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Similarity:  6.2008166 Prediction:  the\n",
      "Similarity:  6.2953067 Prediction:  .\n",
      "Similarity:  6.6166425 Prediction:  of\n",
      "Similarity:  8.115894 Prediction:  to\n",
      "Similarity:  11.023431 Prediction:  in\n",
      "Similarity:  12.648235 Prediction:  government\n",
      "Similarity:  13.668561 Prediction:  city\n",
      "Similarity:  14.845339 Prediction:  police\n",
      "Similarity:  17.281403 Prediction:  capital\n",
      "Similarity:  17.935772 Prediction:  kong\n",
      "Similarity:  20.245106 Prediction:  beijing\n",
      "Similarity:  28.831144 Prediction:  philippines\n",
      "Similarity:  32.45191 Prediction:  manila\n"
     ]
    }
   ],
   "source": [
    "x = np.subtract(x, y)\n",
    "x = np.add(x, z)\n",
    "predict = None\n",
    "max_similarity = 0\n",
    "for word, embedding in embeddings.items():\n",
    "    y = np.array(embedding)\n",
    "    similarity = np.dot(x, y)\n",
    "    if similarity > max_similarity:\n",
    "        predict = word\n",
    "        print(\"Similarity: \", similarity, \"Prediction: \", word)\n",
    "        max_similarity = similarity"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
